/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import pojo.Dondathang;
import pojo.User;
import until.MySqlDataAccessHelper;

/**
 *
 * @author Kute
 */
public class DonDatHang_DAO {

    public static ArrayList<Dondathang> layToanBoCacDonDatHang() {
        ArrayList<Dondathang> ds = new ArrayList<Dondathang>();
        String sql = String.format("select * from dondathang");
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                Dondathang ddh = new Dondathang();
                ddh.setId(rs.getInt("ID"));
                ddh.setMaDeal(rs.getInt("MaDeal"));
                ddh.setMaNguoiMua(rs.getInt("MaNguoiMua"));
                ddh.setSoLuongDeal(rs.getInt("SoLuongDeal"));
                ddh.setTongTien(rs.getDouble("TongTien"));
                ddh.setTenNguoiNhan(rs.getString("TenNguoiNhan"));
                ddh.setTinhTrang(rs.getString("TinhTrang"));
                ddh.setPhuongThucThanhToan(rs.getString("PhuongThucThanhToan"));
                ddh.setNgayDatHang(rs.getDate("NgayDatHang"));
                ddh.setNgaySua(rs.getDate("NgaySua"));
                ddh.setSoThuTuMua(rs.getInt("SoThuTuMua"));
                ddh.setDiaChi(rs.getString("DiaChi"));
                ddh.setDienThoai(rs.getString("DienThoai"));
                ddh.setGhiChu(rs.getString("GhiChu"));
                ds.add(ddh);
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return ds;
    }

    public static ArrayList<Dondathang> layCacDDHTheoMaDeal(int maDeal) {
        ArrayList<Dondathang> ds = new ArrayList<Dondathang>();
        String sql = String.format("select * from dondathang as ddh where ddh.MaDeal=" + maDeal + "");
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                Dondathang ddh = new Dondathang();
                ddh.setId(rs.getInt("ID"));
                ddh.setMaDeal(rs.getInt("MaDeal"));
                ddh.setMaNguoiMua(rs.getInt("MaNguoiMua"));
                ddh.setSoLuongDeal(rs.getInt("SoLuongDeal"));
                ddh.setTongTien(rs.getDouble("TongTien"));
                ddh.setTenNguoiNhan(rs.getString("TenNguoiNhan"));
                ddh.setTinhTrang(rs.getString("TinhTrang"));
                ddh.setPhuongThucThanhToan(rs.getString("PhuongThucThanhToan"));
                ddh.setNgayDatHang(rs.getDate("NgayDatHang"));
                ddh.setNgaySua(rs.getDate("NgaySua"));
                ddh.setSoThuTuMua(rs.getInt("SoThuTuMua"));
                ddh.setDiaChi(rs.getString("DiaChi"));
                ddh.setDienThoai(rs.getString("DienThoai"));
                ddh.setGhiChu(rs.getString("GhiChu"));
                ds.add(ddh);
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return ds;
    }

    public static ArrayList<Dondathang> layCacDDHTheoMaUser(int maUser) {
        ArrayList<Dondathang> ds = new ArrayList<Dondathang>();
        String sql = String.format("select * from dondathang as ddh where ddh.MaNguoiMua=" + maUser + "");
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                Dondathang ddh = new Dondathang();
                ddh.setId(rs.getInt("ID"));
                ddh.setMaDeal(rs.getInt("MaDeal"));
                ddh.setMaNguoiMua(rs.getInt("MaNguoiMua"));
                ddh.setSoLuongDeal(rs.getInt("SoLuongDeal"));
                ddh.setTongTien(rs.getDouble("TongTien"));
                ddh.setTenNguoiNhan(rs.getString("TenNguoiNhan"));
                ddh.setTinhTrang(rs.getString("TinhTrang"));
                ddh.setPhuongThucThanhToan(rs.getString("PhuongThucThanhToan"));
                ddh.setNgayDatHang(rs.getDate("NgayDatHang"));
                ddh.setNgaySua(rs.getDate("NgaySua"));
                ddh.setSoThuTuMua(rs.getInt("SoThuTuMua"));
                ddh.setDiaChi(rs.getString("DiaChi"));
                ddh.setDienThoai(rs.getString("DienThoai"));
                ddh.setGhiChu(rs.getString("GhiChu"));
                ds.add(ddh);
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return ds;
    }

    public static int demSoDonDatHangTheoMaDeal(int maDeal) {
        int dem = 0;
        ArrayList<Dondathang> ds = layCacDDHTheoMaDeal(maDeal);
        for (int i = 0; i < ds.size(); i++) {
            Dondathang dondathang = ds.get(i);
            dem += 1;

        }
        return dem;
    }
    public static int demSoDealTrongDonDatHangTheoMaDeal(int maDeal) {
        int dem = 0;
        ArrayList<Dondathang> ds = layCacDDHTheoMaDeal(maDeal);
        for (int i = 0; i < ds.size(); i++) {
            Dondathang dondathang = ds.get(i);
            dem+=dondathang.getSoLuongDeal();
        }
        return dem;
    }
    
    public static ArrayList<Integer> layDanhSachCacMaDonDatHang() {
      ArrayList<Integer> ds = new ArrayList<Integer>();
      try {
         String sql = "SELECT ID FROM dondathang";
         MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
         helper.open();
         ResultSet rs = helper.executeQuery(sql);
         while (rs.next()) {
            Integer ma = rs.getInt("ID");
            ds.add(ma);
         }
         helper.close();
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      return ds;
   }

   public static int layMaDonDatHangTuDong() {
      int ma = 1;
      ArrayList<Integer> ds =layDanhSachCacMaDonDatHang();
      boolean ok = false;
      for (int i = 0; i < ds.size() - 1; i++) {
         if (ds.get(i + 1) - ds.get(i) > 1) {
            ma = ds.get(i) + 1;
            ok = true;
            break;
         }
      }
      if (ok == false) {
         if (ds.isEmpty()) {
            ma = 1;
         } else {
            ma = ds.get(ds.size() - 1) + 1;
         }
      }
      return ma;
   }
   
    public static int themDonDatHang(Dondathang ddh){
       int maDonDatHang =layMaDonDatHangTuDong();
            String sql=String.format("INSERT INTO dondathang VALUES ('','2', '1', '2', '700000.0', N'phạm minh khuê', N'', '2011-11-04 22:56:02', '2011-11-04 22:56:02', '0', N'', N'01655533550', N'105m/31 ho thi ky p1 quan 1-',N'Đang Chuyển')");
            MySqlDataAccessHelper helper= new MySqlDataAccessHelper();
            helper.open();
            int n=helper.executeUpdate(sql);
            if(n!=1){
                maDonDatHang=-1;
            }
            helper.close();
        return maDonDatHang;
    }
    
    public static String HoanTatDatHang(Dondathang ddh){
        String codes="";
        if(themDonDatHang(ddh)!=-1){
            int maDonDatHang=themDonDatHang(ddh);
           String code="1z12ss2sssas";
            String sql="INSERT INTO code VALUES ('', '"+maDonDatHang+"', N'"+code+"', '"+ddh.getNgayDatHang()+"',"+ddh.getNgayDatHang()+")";
            MySqlDataAccessHelper helper= new MySqlDataAccessHelper();
            helper.open();
            int n=helper.executeUpdate(sql);
            if(n!=1){
                codes=code;
            }
            helper.close();
        }
        return codes;
    }
//    public static int ThemUser(User nguoiDungMoi) {
//        int maKhachHang = 0;
//         int kqkt= KiemTraTrungLap(nguoiDungMoi.getTenDangNhap());
//         if (kqkt==-1) {
//             maKhachHang=-1;
//         }else{
//             try {
//                     String sql = "insert into `user`(TenDangNhap,MatKhau,HoTen,NgaySinh,GioiTinh,Email,DiaChi,DienThoai,HinhDaiDien) values(N'"+nguoiDungMoi.getTenDangNhap()+"',N'"+nguoiDungMoi.getMatKhau()+"',N'"+nguoiDungMoi.getHoTen()+"',N'"+nguoiDungMoi.getNgaySinh()+"',N'"+nguoiDungMoi.getGioiTinh()+"',N'"+nguoiDungMoi.getEmail()+"',N'"+nguoiDungMoi.getDiaChi()+"',"+nguoiDungMoi.getDienThoai()+",N'"+nguoiDungMoi.getHinhDaiDien()+"')";
//                     MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
//                     helper.open();
//                     int n= helper.executeUpdate(sql);
//                     if(n!=1){
//                         maKhachHang=-1;
//                     }
//                     helper.close();
//                  } catch (Exception ex) {
//                     System.out.println(ex.getMessage());
//                  }
//         }
//         
//        return maKhachHang;
//    }
}
